package org.example.examsystem.controller;


import org.example.examsystem.entity.PjClass;
import org.example.examsystem.service.PjClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/classes")
public class PjClassController {

    @Autowired
    private PjClassService pjClassService;

    @GetMapping
    public Page<PjClass> getClasses(@RequestParam(defaultValue = "0") int page,
                                    @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return pjClassService.getClasses(pageable);
    }

    @PostMapping
    public void addClass(@RequestBody PjClass pjClass) {
        pjClassService.addClass(pjClass);
    }

    @PutMapping("/{classid}")
    public void updateClass(@PathVariable int classid, @RequestBody PjClass pjClass) {
        pjClass.setClassid(classid);
        pjClassService.updateClass(pjClass);
    }

    @DeleteMapping("/{classid}")
    public void deleteClass(@PathVariable int classid) {
        pjClassService.deleteClass(classid);
    }

    @GetMapping("/all")
    public List<PjClass> getClassList() {
        return pjClassService.getClassList();
    }

    @GetMapping("/list")
    public List<PjClass> getAllClasses() {
        return pjClassService.getAllClasses();
    }
}
